<?php


namespace App\Http\Controllers\Api;

use App\Businesses\BusinessState;
use App\Constant\ApiV2Constant;
use App\Http\Requests\ApiV2\CommentRequest;
use App\Services\Business\Interfaces\CommentServiceInterface;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Models\User;

use App\Services\Business\Models\Comment;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CommentController extends BaseController
{


    protected $commentService;
    protected $userService;
    protected $businessState;


    public function __construct(
        CommentServiceInterface $commentService,
        UserServiceInterface    $userService,
        BusinessState           $businessState
    )
    {
        $this->commentService = $commentService;
        $this->userService = $userService;
        $this->businessState = $businessState;

    }


    /**
     * 评价列表
     * @param Request $request
     * @param $type
     * @param $id
     * @return JsonResponse
     */
    public function comments(Request $request, $type, $id)
    {

        $size = $request->input('size',10);
        $modal = 'App\Services\Business\Models\\' . ucfirst($type);
        $modal = new $modal;
        $resource = $modal::find($id);
        $comments = $resource->comments()->orderbyDesc('created_at')->where('parent_id',0)->paginate($size);
        $commentUsers = $this->userService->getList(array_column($comments->items(), 'user_id'));
        $commentUsers = arr2_clear($commentUsers, ApiV2Constant::MODEL_MEMBER_FIELD);
        $commentUsers = array_column($commentUsers, null, 'id');

        return $this->data([
            'comments' => $comments,
            'users' => $commentUsers,
        ]);
    }


    /**
     * 提交评价
     * @param CommentRequest $request
     * @param string $type
     * @param int $id
     * @return JsonResponse
     */
    public function submitComment(CommentRequest $request, string $type, int $id)
    {

        $data = $request->filldata();
        $comment =  $this->commentService->create($type,$id, $data);
        $comment_id = $comment['id'];
        $reply_id = $comment['reply_id'];
        return $this->data(compact('comment_id','reply_id'));
    }

    /**
     *评价的子评价
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function answerComments(Request $request, $id)
    {

        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $parentId = $request->input('parent_id', 0);
        $list = Comment::query()->where('parent_id', $parentId)
            ->with(['user', 'reply'])
            ->orderByDesc('created_at')
            ->forPage($page, $size)->get()->toArray();

        return $this->data($list);


    }

    /**
     * 删除评价
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function delete(Request $request, $id)
    {

        Comment::destroy($id);

        return $this->success(__('删除成功'));
    }
}
